//
//  GQNavigationController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/4.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQNavigationController.h"
#import "UIBarButtonItem+Item.h"
#import "GQTabBar.h"

@interface GQNavigationController () <UINavigationControllerDelegate>

@property (strong, nonatomic) id popDelegate;

@end

@implementation GQNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17], NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationBar.barTintColor = kNavigationBarColor;
    _popDelegate = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;
    self.title = @"123";
}

- (void)backItemDidClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

//- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
//{
//    if (self.childViewControllers.count) {
//        viewController.hidesBottomBarWhenPushed = YES;
//        UIBarButtonItem *left = [UIBarButtonItem barButtonItemWithItem:[UIImage imageNamed:@"distribute_back"] highImage:[UIImage imageNamed:nil] target:self action:@selector(popToPre) forControlEvents:UIControlEventTouchUpInside];
//        viewController.navigationItem.leftBarButtonItem = left;
//    }
//    [self pushViewController:viewController animated:animated];
//    
//}
//
//- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
//{
//    if (viewController == self.viewControllers[0]) {
//        self.interactivePopGestureRecognizer.delegate = nil;
//    }else{
//        self.interactivePopGestureRecognizer.delegate = _popDelegate;
//    }
//}
//
//- (void)popToPre
//{
//    [self popViewControllerAnimated:YES];
//}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//    NSLog(@"%d", self.childViewControllers.count);
//    if (self.childViewControllers.count == 0) {
//        self.navigationBar.hidden = YES;
//    }
//    if (self.childViewControllers.count) { // 不是根控制器
    NSLog(@"qqq");
       // viewController.hidesBottomBarWhenPushed = YES;
//        self.navigationBar.hidden = NO;
        UIBarButtonItem *left = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"distribute_back"] highImage:nil target:self action:@selector(popToPre) forControlEvents:UIControlEventTouchUpInside];
        // 设置导航条的按钮
        viewController.navigationItem.leftBarButtonItem = left;
        
//        UIBarButtonItem *right = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"distribute_back"] highImage:nil target:self action:@selector(popToRoot) forControlEvents:UIControlEventTouchUpInside];
//        viewController.navigationItem.rightBarButtonItem = right;
//    }
    
    [super pushViewController:viewController animated:animated];
    
}

//- (void)popToRoot
//{
//    [self popToRootViewControllerAnimated:YES];
//}
- (void)popToPre
{
    [self popViewControllerAnimated:YES];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//    UITabBarController *tabBarVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//    
//    // 删除系统自带的tabBarButton
//    for (UIView *tabBarButton in tabBarVc.tabBar.subviews) {
//        if (![tabBarButton isKindOfClass:[GQTabBar class]]) {
//            [tabBarButton removeFromSuperview];
//        }
//    }
    
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == self.viewControllers[0]) { // 是根控制器
        
        self.interactivePopGestureRecognizer.delegate = nil;
        
    }else{ // 非根控制器
        self.interactivePopGestureRecognizer.delegate = _popDelegate;
        
    }
}

@end
